package com.test.poi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class MyCreatExcel {
	public static void main(String[] args) throws IOException {
		HSSFWorkbook wb = new HSSFWorkbook();

		    // 创建Excel的工作sheet,对应到一个excel文档的tab
		
		    // 创建Excel的工作sheet,对应到一个excel文档的tab
		 
		Object[] strs = {"aa",111,new Date(),"dd"};
		Object[] strs2 = {"aa",3333.888,"cc","dd"};
		List<Object[]> list = new ArrayList<Object[]>();
		list.add(strs);
		list.add(strs2);
		wb = createSheet(list,wb);
		FileOutputStream os = new FileOutputStream("H:\\lei.xls");
        wb.write(os);
        os.close();
	}
	
	
	
	static HSSFWorkbook createSheet(List<Object[]> list,HSSFWorkbook wb){
		HSSFSheet sheet = wb.createSheet("sheet1");
		HSSFRow row =null;
		HSSFCell cell = null;
		for(int rownum=0;rownum<list.size();rownum++ ){
			row = sheet.createRow(rownum);
			Object[] values  = list.get(rownum);
			for(int colnum=0;colnum<values.length;colnum++){
				cell = row.createCell(colnum);
				Object obj = values[colnum];
				if(obj instanceof String){
					cell.setCellValue((String)values[colnum]);
				}else if (obj instanceof Double){
					cell.setCellValue((Double)values[colnum]);
				}else if (obj instanceof Integer){
					cell.setCellValue((Integer)values[colnum]);
				}else if (obj instanceof Float){
					cell.setCellValue((Float)values[colnum]);
				}else if (obj instanceof Date){
					System.out.println("----");
					cell.setCellValue((Date)values[colnum]);
				}
			}
		}
		return wb;
		
	}
	
}
